--
-- Author: yawei.zhu
-- Date: 2018-12-14 16:20:27
--
-- PvpTiantiPanel
--

local NewDungeonTypeRender = require("app.team.views.renders.NewDungeonTypeRender")
local PvpLadderBottom = import(".PvpLadderBottom")

local ClassRef = app.mvc.createPanelClass(...)

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.PvpTiantiNode))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)

    self._ladderList = self._ui.ListView
    self._ladderList.createRender = function (list, idx)
        local items = list:getItems()
        local render = items[idx]
        if nil == render then
            render = NewDungeonTypeRender.create()
            list:pushBackCustomItem(render)
        end
        render:setVisible(true)
        return render
    end

    self._pvpLadderBottom = PvpLadderBottom.new(self._ui.Panel_bom, self)
    self._pvpLadderBottom:onOperate(function(op, data)
        executeFunc(self._onOperate, op, data)
    end)
end

function ClassRef:refresh()
    if not self._data then return end
end

function ClassRef:updateView(pvpInfo, openLvDict)
    if pvpInfo == nil then
        return
    end
    -- 清空list
    local list = self._ladderList
    list:removeAllChildren()

    local datas = GD:queryTeamTargetSurfacesByType(Enums.TeamMatchType.Pvp)
    for i,data in ipairs(datas) do
        local targetId = tonumber(data.ID)
        local render = list:createRender(i)
        render:setType(Enums.TeamMatchType.Pvp, openLvDict[targetId] or 99)
        render:setData(data)
        render:setFirstWin(data.enable == "1" and pvpInfo:isFirstWin(targetId))
        render:onBtnClick(function(sender)
            if data.enable == "1" then
                UIHandler:handleMessage({id=Constant.FUNC_TYPE_TEAMMATCHING, mode="open", data={targetId=targetId}})
            else
                display.pushToast( L("lua_code_text_458") )
            end
        end)
    end

    if pvpInfo:hasLastReward() then
        self:refreshLadder(pvpInfo)
        -- self:openLevelView(pvpInfo.lastSeasonInfo, 3)
        executeFunc(self._onOperate, "openLevelView", {info = pvpInfo.lastSeasonInfo, type = 3})
    elseif pvpInfo:hasChangeCache() or UD:hasArenaLuckBoxCache() then
        local starData, stepDatas = pvpInfo:getChangeData()
        self:refreshLadder(pvpInfo, true)
        self:playChangeAction(starData, stepDatas, function(actionName)
            -- action 分两部分，star和level，分别监听细化处理
            local preLev, curLev = 1, 1
            if #stepDatas > 0 then
                preLev = stepDatas[1].level
                curLev = stepDatas[#stepDatas].level
            end
            if actionName == "level" then
                -- 等级越低，越好
                if preLev > curLev then
                    -- 升级
                    -- self:openLevelView(pvpInfo.seasonInfo, 1)
                    executeFunc(self._onOperate, "openLevelView", {info = pvpInfo.seasonInfo, type = 1})
                elseif preLev < curLev then
                    -- 降级
                    -- self:openLevelView(pvpInfo.seasonInfo, 2)
                    executeFunc(self._onOperate, "openLevelView", {info = pvpInfo.seasonInfo, type = 2})
                end
            elseif actionName == "star" then
                if preLev == curLev then
                    self:checkGuide()
                end
            end
        end)
    else
        self:checkGuide()
        self:refreshLadder(pvpInfo)
    end
end

-- 天梯赛接口
function ClassRef:refreshLadder(data, withAction)
    local season = data.seasonInfo
    self._ui.title:setString(season:getTitle())
    self._ui.data:setString(season:getDateStr())
    self._pvpLadderBottom:refresh(data, withAction)
end

function ClassRef:playChangeAction(starData, stepDatas, cb)
    self._isInStarAction = true
    self._isInLevelAction = true
    self._pvpLadderBottom:playChangeAction(starData, stepDatas, function(actionName)
        -- action 分两部分，star和level，分别监听细化处理
        if actionName == "level" then
            self._isInLevelAction = false
        elseif actionName == "star" then
            self._isInStarAction = false
            if self._hasStarGuide then
                self:checkGuide()
            end
        end
        executeFunc(cb, actionName)
    end)
end

-- 检查是否触发了引导
function ClassRef:checkGuide()
    if self._isInStarAction then 
        self._hasStarGuide = true
        return
    end

    local funcId = Constant.FUNC_TYPE_PVP
    local params = {
        scene = funcId,
        sceneSelf = self,
        getFunc = self.getGuideNodeFunc,
        nextFunc = self.checkGuide
    }
    EM:notify("checkGuide", params)
end

-- 获取引导node
function ClassRef:getGuideNodeFunc(id)
    if id == 162 then
        -- 选择 锦标赛-竞技场
        local item = self._ladderList:getItem(0)
        if item then
            return item:getChildByName("Panel")
        end
    elseif id == 168 or id == 170 then
        -- 选择 锦标赛-宝箱
        return self._pvpLadderBottom:getBox()
    end
end

function ClassRef:onOperate(cb)
    self._onOperate = cb
end

return ClassRef
